<template>
  <div>
    <div class="search-container">
      <el-input v-model="keyword" placeholder="请通过用户名搜索..." prefix-icon="el-icon-search" style="width: 400px;margin-right: 8px"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="doSearch">搜索</el-button>
    </div>
    <div class="admin-container">
      <el-card class="admin-card" v-for="(admin,index) in admins" :key="index">
        <div slot="header" class="clearfix">
          <span>{{admin.name}}</span>
          <el-button style="float: right; padding: 3px 0;color: red" type="text" icon="el-icon-delete" @click="deleteAdmin(admin)"></el-button>
        </div>
       <div class="img-container">
         <img src="../../assets/userFace.jpg" :title="admin.name" :alt="admin.name" class="admin-img">
       </div>
        <div>
          <div>电话号码：{{admin.telephone}}</div>
          <div>手机号：{{admin.phone}}</div>
          <div>地址：{{admin.address}}</div>
          <div>用户状态：
            <el-switch
              v-model="admin.enabled"
              @change="enableAdmin(admin)"
              active-color="#13ce66"
              inactive-color="#c0c0c0">
            </el-switch>
          </div>
          <div>用户名：{{admin.username}}</div>
          <div>
            角色：
            <el-tag v-for="(role,indexj) in admin.roles" :key="indexj" style="margin-left:4px">{{role.namezh}}</el-tag>
            <el-popover
                placement="right"
                title="角色列表"
                width="200"
                @show="showRoles(admin)"
                @hide="updateRoles(admin)"
                trigger="click">
              <el-select v-model="selectedRoles" multiple placeholder="请选择用户角色">
                <el-option
                    v-for="(role,index) in allRoles"
                    :key="index"
                    :label="role.namezh"
                    :value="role.id">
                </el-option>
              </el-select>
              <el-button type="text" icon="el-icon-more" slot="reference"></el-button>
            </el-popover>


          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: "SysAdmin",
  data() {
    return {
      admins:[],
      keyword:'',
      selectedRoles:[],
      allRoles:[]
    }
  },
  mounted() {
    this.initAdmins();
  },
  methods:{

    /**
     * 更新角色
     */
    updateRoles(admin) {
      // 判断角色是否刷新
        let flag = false;
        let roles = [];
        // 复制角色
        Object.assign(roles,admin.roles);
        if (roles.length !== this.selectedRoles.length) {
          flag = true;
        }else {
          // 长度相等的处理
          for (let i = 0; i < roles.length; i++) {
              let role = roles[i];
            for (let j = 0; j < this.selectedRoles.length; j++) {
               let roleId = this.selectedRoles[j];
               if (role.id === roleId) {
                  roles.splice(i,1);
                  i--;
                  break;
               }
            }
          }
          if (roles.length !== 0) {
            flag = true;
          }
        }

        if (flag) {
          let url = "/system/admin/roles?adminId=" + admin.id;
          this.selectedRoles.forEach(id => {
            url += "&rids=" + id;
          })
          this.putRequest(url).then(resp => {
            if (resp) {
              this.initAdmins();
            }
          })
        }
    },



    /**
     *  显示角色
     */
    showRoles(admin) {
      // 在选择框显示角色
      let roles = admin.roles;
      // 初始化角色selectedRoles
      this.selectedRoles = [];
      roles.forEach(r => {
          this.selectedRoles.push(r.id);
      })

      this.initRoles();
    },

    /**
     * 查询所有角色
     */
    initRoles() {
      this.getRequest("/system/admin/roles").then(resp => {
        if (resp) {
          this.allRoles = resp;
        }
      })
    },

    /**
     *  用户禁用
     */
    enableAdmin(admin) {
      this.putRequest("/system/admin/",admin).then(res => {
        if (res) {
          this.initAdmins();
        }
      })
    },

    /**
     * 删除
     */
    deleteAdmin(admin) {
      this.$confirm('此操作将永久删除['+ admin.name +']用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/admin/' + admin.id).then(resp => {
          if (resp) {
            this.initAdmins();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    /**
     * 搜索
     */
    doSearch() {
      this.initAdmins();
    },


    /**
     * 查询所有操作员
     */
    initAdmins() {
      this.getRequest('/system/admin/?keywords='+this.keyword).then(resp => {
        if (resp) {
          this.admins = resp;
        }
      })
    }
  }
}
</script>

<style scoped>
    .search-container {
      display: flex;
      justify-content: center;
      margin-top: 10px;
      margin-bottom: 20px;
    }

    .admin-container {
      display: flex;
      justify-content: space-around;
      flex-wrap: wrap;
    }

    .admin-card {
      width: 500px;
      margin-bottom: 20px;
    }

    .img-container {
      display: flex;
      justify-content: center;
    }

    .admin-img {
      width: 70px;
      height: 70px;
      border-radius: 2em;
    }

</style>